package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yf.mall.common.Const;
import yf.mall.entity.*;
import yf.mall.enums.*;
import yf.mall.service.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * quequnlong
 */
@RequestMapping("/api/saleTypes")
@RestController
public class ApiSaleTypesController {
    @Autowired
    private SaleTypesService saleTypesService;
    @Autowired
    private MallGoodsService goodsService;
    @Autowired
    private SaleTypeInfoService saleTypeInfoService;

    @Autowired
    private MallGoodsService mallGoodsService;

    @GetMapping("/list")
    public R list(@LoginUserId long userId) {
        List<SaleTypesEntity> saleTypesEntities = saleTypesService.findAllAsc();
        List<MallGoodsEntity> dailyGoodslist;
        List<MallGoodsEntity> storeGoodsList;
        //每日优选
        dailyGoodslist = mallGoodsService.findDailyList(MallGoodsShelfEnum.ON.getIndex(), MallGoodsDailySelEnum.OPEN.getIndex(),MallGoodsDailySelEnum.OPEN.getIndex());
        // 分享有利
        Map hashMap = new HashMap<>();
        hashMap.put("shelf", MallGoodsShelfEnum.ON.getIndex());//已上架
        hashMap.put("saleType", MallGoodsSaleTypeEnum.RETAIL.getIndex());
        storeGoodsList = mallGoodsService.queryList(hashMap);
        storeGoodsList = mallGoodsService.filterByLevel(userId, storeGoodsList);
        //筛选开启轮播的
        storeGoodsList = storeGoodsList.stream().filter(g->g.getDailySelRotation()!=null&&g.getDailySelRotation().equals(MallGoodsDailySelEnum.OPEN.getIndex())).collect(Collectors.toList());
        return R.ok(saleTypesEntities).putExtra("dailyList", dailyGoodslist).putExtra("storeList", storeGoodsList);
    }

    /**
     * quequnlong
     * 根据功能标签id查找商品
     *
     * @param saleType
     * @return
     */
    @GetMapping("/BySaleTypeId")
    public R findGoodsBySaleTypeId(@LoginUserId Long userId, int page, int limit, @RequestParam(required = false) Long type,
                                   Integer saleType, @RequestParam(required = false) String order, String shopName) {
        //如果是智慧门店 则查找是智慧门店的店铺
        List<ShopEntity> shopEntity = null;
        List<MallGoodsEntity> mallGoodsEntities = null;
        if (saleType == MallGoodsSaleTypeEnum.WISDOM.getIndex()) {
            PageHelper.startPage(page, limit);
            Map<String, Object> map = new HashMap<>();
            map.put("type", ShopTypeEnum.ONE.getIndex());
            map.put("status", ShopStatusEnum.PASS.getIndex());
            if (shopName != null && !"".equals(shopName)) {
                map.put("name", shopName);
            }
            shopEntity = shopService.queryList(map);
        }else if(saleType == MallGoodsSaleTypeEnum.DAY_SEL.getIndex()){
            //查询每日优选的
            PageHelper.startPage(page, limit);
            Map<String, Object> map = new HashMap<>();
            map.put("dailySel", 1);
            if (shopName != null && !"".equals(shopName)) {
                map.put("name", shopName);
            }
            shopEntity = shopService.queryList(map);
        } else {
            if (order == null) {
                order = "sort,id Asc";
            }
            //Sort正排序  ID反排序
            PageHelper.startPage(page, limit).setOrderBy(order);
            Map<String, Object> map = new HashMap<>();
            //是首页 不做分类查询 0为首页推荐
            if (type == 0) {
                type = null;
            }
            map.put("saleType", saleType);
            map.put("shelf", MallGoodsShelfEnum.ON.getIndex());

            mallGoodsEntities = goodsService.findBySaleType(map, type);
            mallGoodsEntities = goodsService.filterByLevel(userId, mallGoodsEntities);
            //获取活动
            for (MallGoodsEntity mallGoodsEntity : mallGoodsEntities) {
                SaleTypeInfoEntity saleTypeInfoEntity = saleTypeInfoService.findByGoodsId(mallGoodsEntity.getId());
                if (null != saleTypeInfoEntity) {
                    mallGoodsEntity.setAct(saleTypeInfoEntity);
                }
            }
            //获取所属店铺
            getShop(mallGoodsEntities);
            //排序
            sort(mallGoodsEntities);
        }

        return R.ok(mallGoodsEntities).putExtra("shop", shopEntity);
    }


    /**
     * 每日优选
     *
     * @return
     */
    @RequestMapping("/daily")
    public R daily(@LoginUserId Long userId, int page, int limit, @RequestParam(required = false) String order) {
        //如果是智慧门店 则查找是智慧门店的店铺
        List<ShopEntity> shopEntity = null;
        List<MallGoodsEntity> mallGoodsEntities;

        if (order == null) {
            order = "sort,id Asc";
        }
        //Sort正排序  ID反排序
        PageHelper.startPage(page, limit).setOrderBy(order);
        //获取每日优选产品
        mallGoodsEntities = mallGoodsService.daily();
        //进行筛选
        mallGoodsEntities = goodsService.filterByLevel(userId, mallGoodsEntities);
        //获取活动
        for (MallGoodsEntity mallGoodsEntity : mallGoodsEntities) {
            SaleTypeInfoEntity saleTypeInfoEntity = saleTypeInfoService.findByGoodsId(mallGoodsEntity.getId());
            if (null != saleTypeInfoEntity) {
                mallGoodsEntity.setAct(saleTypeInfoEntity);
            }
        }
        //获取所属店铺
        getShop(mallGoodsEntities);
        //排序
        sort(mallGoodsEntities);

        return R.ok(mallGoodsEntities).putExtra("shop", shopEntity);
    }

    @Autowired
    private ShopService shopService;

    //quequnlong 获取所属店铺
    private void getShop(List<MallGoodsEntity> mallGoodsEntities) {
        for (MallGoodsEntity mallGoodsEntity : mallGoodsEntities) {
            Long userId = mallGoodsEntity.getUserId();
            //不是公司的产品
            if (userId != null && !userId.equals(Const.ADMIN_ID)) {
                ShopEntity shopEntity = shopService.findByUserId(userId);
                if (shopEntity != null) {
                    mallGoodsEntity.setShopEntity(shopEntity);
                }
            }
        }
    }

    //排序
    private void sort(List<MallGoodsEntity> list) {
        Collections.sort(list, (o1, o2) -> {
            if (null != o1.getSort() && null != o2.getSort()) {
                if (o1.getSort() > o2.getSort()) {
                    return 1;
                }
            }
            return -1;
        });
    }
}
